package com.example.answer_staff;

import org.apache.http.Header;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.answer_staff.api.ApiManager;
import com.gaosai.staff.domain.MessageResult;
import com.gaosai.staff.utils.CommonUtils;
import com.gaosai.staff.utils.SPUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class ChangePasswordActivity  extends Activity{

	private ImageView back;
	private Button bt_OK;
	private EditText current_password;
	private EditText New_password;
	private EditText Confirm_password;
//	private String barCode_ChangePassword;
//	private String[] barcode_string;
	private String PassWord;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.change_password);
		setupView();
		setListeners();
	}

	private void setupView() {
		
		PassWord=SPUtil.getValue(getApplicationContext(), SPUtil.PASSWORD);
//		barCode_ChangePassword=SPUtil.getValue(getApplicationContext(), SPUtil.BARCODE);
		//从barcode_string可以得到userName；
//		barcode_string=JsonUtils.ParseQr(barCode_ChangePassword);
		back=(ImageView)findViewById(R.id.backsetting);
		bt_OK=(Button)findViewById(R.id.bt_OK);
		current_password=(EditText)findViewById(R.id.current_password);
		New_password=(EditText)findViewById(R.id.New_password);
		Confirm_password=(EditText)findViewById(R.id.Confirm_password);
	}
	private void setListeners() {
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(ChangePasswordActivity.this,SettingActivity.class));
				finish();
			}
		});
		bt_OK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String oldPassword = current_password.getText().toString();
				String password1 = New_password.getText().toString();
				final String repassword1 = Confirm_password.getText().toString();
				boolean result_bolean = CommonUtils.accordance(password1,repassword1,ChangePasswordActivity.this);
			if(!oldPassword.isEmpty()&!password1.isEmpty()&!repassword1.isEmpty()){
				
				if(result_bolean){
					/**修改用户密码
					 * */
					if(oldPassword.equals(PassWord)){
					ApiManager.getInstance().changePasword(ChangePasswordActivity.this,SPUtil.getValue(getApplicationContext(), SPUtil.UserName), PassWord, repassword1,new AsyncHttpResponseHandler() {
						@Override
						public void onFailure(int arg0, Header[] arg1, byte[] arg2,
								Throwable arg3) {
						}
						@Override
						public void onFinish() {
							super.onFinish();
						}

						@Override
						public void onStart() {
							super.onStart();
						}
						@Override
						public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
							Log.e("eee", "ChangeACtivity界面的密码是"+SPUtil.getValue(getApplicationContext(), SPUtil.PASSWORD));
							SPUtil.putValue(getApplicationContext(), SPUtil.PASSWORD, repassword1);
							String result = new String(arg2) ;
							Log.e("eee", "changePassword...........是"+result);
							MessageResult messageResult = MessageResult.prase(result);
							if(0==messageResult.getCode()){
								Toast.makeText(ChangePasswordActivity.this, messageResult.getMsg(), 0).show() ;
								//								startActivity(new Intent(ChangePasswordActivity.this,LoginActivity.class));
							}else{
								Toast.makeText(ChangePasswordActivity.this, messageResult.getMsg(), 0).show() ;
							}
						}
					}) ;

				}else{
					Toast.makeText(getApplicationContext(), "旧密码不正确，请重新输入",1).show();
				}
			}
			
			}else{
				Toast.makeText(getApplicationContext(), "输入不能为空，请重新输入",1).show();
			}
			
			
			
			
			
			}
			});

	}
}


